import 'dart:convert';
import 'dart:ui';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:h_flutter_119/api/Api.dart';
import 'package:h_flutter_119/http/httpUtils.dart';
import 'package:h_flutter_119/page/find/model/Focus_Entity.dart';
import 'package:h_flutter_119/page/find/model/Focus_Item_Entity.dart';
import 'package:h_flutter_119/page/rank/child/RankItemWidget.dart';

class EyeRankPage extends StatefulWidget {

  String strategy;

  EyeRankPage(this.strategy, {Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => _EyeRankPageState();

}

class _EyeRankPageState extends State<EyeRankPage> {

  List<FocusItemEntity> items = [];
  late EasyRefreshController _controller;
  late ScrollController _scrollController;
  bool isShowGoTop = false;

  @override
  void initState() {
    super.initState();
    _scrollController = ScrollController();
    _controller = EasyRefreshController(
        controlFinishLoad: true,
        controlFinishRefresh: true
    );
    _scrollController.addListener(() {
      setState((){
        isShowGoTop = _scrollController.offset >= MediaQueryData.fromWindow(window).size.height / 2 ;
      });
    });
    _controller.finishLoad(IndicatorResult.noMore);
    getNetData();
  }


  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
    _scrollController.dispose();
  }

  void getNetData() async{

    var response = await HttpUtils.getInstance().get(Api.EYE_RANK_LIST,data: {"strategy":widget.strategy});
   var entity = FocusEntity.fromJson(json.decode(response.toString()));

   setState((){
     items.addAll(entity.itemList??[]);
   });

  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: Visibility(
        visible: isShowGoTop,
        child: FloatingActionButton(
          onPressed: () {
            _scrollController.animateTo(0, duration: const Duration(milliseconds: 200), curve: Curves.ease);
          },
          backgroundColor: Colors.red,
          child: const Icon(Icons.arrow_upward),
        ),
      ),
      body: EasyRefresh.builder(
        controller: _controller,
        header: const PhoenixHeader(),
        footer: const PhoenixFooter(),
        onRefresh: () async{
          getNetData();
        },
        // onLoad: () async{
        //
        // },
        childBuilder: (context, physics) =>
          ListView.builder(itemBuilder: (context, index) {
            return RankItemWidget(items[index]);
          },controller: _scrollController, physics: physics, itemCount: items.length),),
    );
  }

}